package com.example.springbott.exception;

import com.example.springbott.bean.BaseRespVo;
import com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException;
import com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 异常捕获处理类
 * @author silic xia
 * @since 2022/11/21 16:58
 */
@RestControllerAdvice
public class ExceptionControllerAdvice {

    @ExceptionHandler({HttpMessageNotReadableException.class,NumberFormatException.class})
    public BaseRespVo parametersException(Exception exception){
        System.out.println("异常捕获ExceptionHandler: " + exception.getMessage());
        return BaseRespVo.invalidParameter402("参数不对！");
    }
    @ExceptionHandler({MySQLIntegrityConstraintViolationException.class})
    public BaseRespVo registerException(Exception exception){
        System.out.println("异常捕获ExceptionHandler: " + exception.getMessage());
        return BaseRespVo.invalidParameter402("用戶名重複！");
    }
    @ExceptionHandler({MySQLSyntaxErrorException.class})
    public BaseRespVo cartException(Exception exception){
        System.out.println("异常捕获ExceptionHandler: " + exception.getMessage());
        return BaseRespVo.invalidParameter402("购物车为空！");
    }
    // @ExceptionHandler({NullPointerException.class})
    // public BaseRespVo loginException(Exception exception){
    //     System.out.println("异常捕获ExceptionHandler: " + exception.getMessage());
    //     return BaseRespVo.invalidParameter402("请先登录！");
    // }


}

